package cn.iocoder.yudao.module.wms.service.warehouse;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.wms.controller.admin.warehouse.vo.WarehousePageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.warehouse.vo.WarehouseSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.warehouse.WarehouseDO;
import cn.iocoder.yudao.module.wms.dal.mysql.warehouse.WarehouseMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.WAREHOUSE_EXISTS;
import static cn.iocoder.yudao.module.wms.enums.ErrorCodeConstants.WAREHOUSE_NOT_EXISTS;

/**
 * 仓库档案 Service 实现类
 *
 * @author 刘锦
 */
@Service
@Validated
public class WarehouseServiceImpl implements WarehouseService {

    @Resource
    private WarehouseMapper warehouseMapper;

    @Override
    public Integer createWarehouse(WarehouseSaveReqVO createReqVO) {
        // 查询编码是否存在
        validateCodeExists(createReqVO.getCode());
        // 插入
        WarehouseDO warehouse = BeanUtils.toBean(createReqVO, WarehouseDO.class);
        warehouseMapper.insert(warehouse);
        // 返回
        return warehouse.getId();
    }



    @Override
    public void updateWarehouse(WarehouseSaveReqVO updateReqVO) {
        // 校验存在
        WarehouseDO warehouseDO = validateWarehouseExists(updateReqVO.getId());

        // 判断是否更新了编码
        String code = warehouseDO.getCode();
        if (!Objects.equals(code, updateReqVO.getCode())) {
            // 校验编码是否存在
            validateCodeExists(updateReqVO.getCode());
        }
        // 更新
        WarehouseDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseDO.class);
        warehouseMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouse(Integer id) {
        // 校验存在
        validateWarehouseExists(id);
        // 删除
        warehouseMapper.deleteById(id);
    }

    private void validateCodeExists(String code) {
        LambdaQueryWrapper<WarehouseDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WarehouseDO::getCode, code);
        WarehouseDO warehouseDO = warehouseMapper.selectOne(queryWrapper);
        if (warehouseDO!=null) {
            throw exception(WAREHOUSE_EXISTS);
        }
    }
    private WarehouseDO validateWarehouseExists(Integer id) {
        if (warehouseMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_NOT_EXISTS);
        }
        return warehouseMapper.selectById(id);
    }

    @Override
    public WarehouseDO getWarehouse(Integer id) {
        return warehouseMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseDO> getWarehousePage(WarehousePageReqVO pageReqVO) {
        return warehouseMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarehouseDO> getWarehouseSimpleList() {
        return warehouseMapper.selectList();
    }

    @Override
    public List<WarehouseDO> getList() {
        return warehouseMapper.selectList();
    }

}